package com.allprs.hmcs.service.impl;

import com.allprs.common.core.domain.AjaxResult;
import com.allprs.common.utils.StringUtils;
import com.allprs.hmcs.entity.*;
import com.allprs.hmcs.mapper.*;
import com.allprs.hmcs.pojo.reqBo.*;
import com.allprs.hmcs.service.IBuildManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wjf
 * @描述:
 * @date 2024年06月13日
 * @version: 1.0
 */

@Service
public class BuildManageServiceImpl implements IBuildManageService {

    @Resource
    private PondingPointMapper pondingPointMapper;

    @Resource
    private SewageMapper sewageMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private DevicePtifallMapper devicePtifallMapper;

    @Resource
    private MaterialMapper materialMapper;


    @Override
    public AjaxResult getPondingInfo(PondingQueryBo queryBo) {
        QueryWrapper<PondingPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                .eq(queryBo.getId() != 0, "id", queryBo.getId());
        List<PondingPoint> pondingPoints = pondingPointMapper.selectList(queryWrapper);

        return AjaxResult.success(pondingPoints);
    }

    @Override
    public AjaxResult getSewageInfo(SewageQueryBo queryBo) {
        QueryWrapper<Sewage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                .eq(queryBo.getId() != 0, "object_id", queryBo.getId());
        List<Sewage> sewages = sewageMapper.selectList(queryWrapper);

        return AjaxResult.success(sewages);
    }

    @Override
    public AjaxResult addProject(Project vo) {
        int i = projectMapper.insert(vo);
        return i > 0 ? AjaxResult.success("新增成功！"):AjaxResult.error("新增失败！");

    }

    @Override
    public AjaxResult getProjectInfo(ProjectQueryBo queryBo) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                .eq(queryBo.getId() != 0, "id", queryBo.getId());
        List<Project> projects = projectMapper.selectList(queryWrapper);

        return AjaxResult.success(projects);
    }

    @Override
    public AjaxResult getDeviceInfo(DeviceQueryBo queryBo) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                .eq(StringUtils.isNotBlank(queryBo.getType()), "type", queryBo.getType())
                .eq(queryBo.getId() != 0, "id", queryBo.getId());

        List<Device> deviceList = deviceMapper.selectList(queryWrapper);

        if(CollectionUtils.isNotEmpty(deviceList)){
            for (Device device : deviceList) {
                QueryWrapper<DevicePitfall> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq(device.getId() != 0, "device_id", device.getId());
                List<DevicePitfall> devicePitfallList = devicePtifallMapper.selectList(queryWrapper1);
                device.setPitfallList(devicePitfallList);
            }
        }
        return AjaxResult.success(deviceList);
    }

    @Override
    public AjaxResult getMaterialInfo(MaterialQueryBo queryBo) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                .eq(queryBo.getId() != 0, "id", queryBo.getId());
        List<Material> materialList = materialMapper.selectList(queryWrapper);

        return AjaxResult.success(materialList);
    }

}
